/**
 * Created by hubin on 2016/6/14.
 */
var Count_vm = avalon.define({
    $id: "count",
    pageSize: 6,
    pageCurrent: 1,
    pageTotle: 0,
    pageOrderSize: 6,
    pageOrderCurrent: 1,
    pageOrderTotle: 0,
    Orderlist: {},
    currentItem: {},
    currentin: 0,
    currentout: 0,
    keyword: "",
    userItem: {},

    load: function () {
        $.ajax({
            type: 'POST',
            url: '/CounterDrops/weOrder/orderlistByUser',
            data: {
                pageSize: 10000,
                pageCurrent: 1,
                userId: Count_vm.currentItem.userid
            },
            success: function (data) {
                var list = data.model.results;
                Count_vm.currentin = 0;
                Count_vm.currentout = 0;
                for (var i = 0; i < list.length; i++) {
                    if (list[i].incometype == '收入') {
                        Count_vm.currentin = Count_vm.currentin + list[i].charges;
                    } else if (list[i].incometype == '支出') {
                        Count_vm.currentout = Count_vm.currentout + list[i].charges;
                    }
                }
                var data = [];
                data[0] = {label: "收入", data: Count_vm.currentin};
                data[1] = {label: "支出", data: Count_vm.currentout};
                var pie = $.plot($(".pie"), data, {
                    series: {
                        pie: {
                            show: true,
                            radius: 3 / 4,
                            label: {
                                show: true,
                                radius: 3 / 4,
                                formatter: function (label, series) {
                                    return '<div style="font-size:8pt;text-align:center;padding:2px;color:white;">' + label + '<br/>' + Math.round(series.percent) + '%</div>';
                                },
                                background: {
                                    opacity: 0.5,
                                    color: '#000'
                                }
                            },
                            innerRadius: 0.2
                        },
                        legend: {
                            show: false
                        }
                    }
                });
            },
            error: function (e) {
            }
        })
    },
    getUserList: function () {
        $.ajax({
            type: 'POST',
            url: '/CounterDrops/weuser/getuserList',
            data: {
                pageSize: Count_vm.pageSize,
                pageCurrent: Count_vm.pageCurrent,
            },
            success: function (data) {
                Count_vm.userItem = data.model.results;
                Count_vm.pageTotle = data.model.totalPage;

            },
            error: function (e) {
            }
        })
    },
    cheakOder: function (el) {
        Count_vm.currentItem = el;
        Count_vm.load();
        $.ajax({
            type: 'POST',
            url: '/CounterDrops/weOrder/orderlistByUser',
            data: {
                pageSize: Count_vm.pageOrderSize,
                pageCurrent: Count_vm.pageOrderCurrent,
                userId: Count_vm.currentItem.userid
            },
            success: function (data) {
                Count_vm.Orderlist = data.model.results;
                Count_vm.pageOrderTotle = data.model.totalPage;
            },
            error: function (e) {
            }
        })
    },
    OrderfirstPage: function () {
        Count_vm.pageOrderCurrent = 1;
        Count_vm.cheakOder(Count_vm.currentItem);
    },
    firstPage: function () {
        Count_vm.pageCurrent = 1;
        Count_vm.getUserList();
    },
    OrderlastPage: function () {
        Count_vm.pageOrderCurrent = Count_vm.pageOrderTotle;
        Count_vm.cheakOder(Count_vm.currentItem);
    },
    lastPage: function () {
        Count_vm.pageCurrent = Count_vm.pageTotle;
        Count_vm.getUserList();
    },
    OrderpreviousPage: function () {
        if (Count_vm.pageOrderCurrent == 1) {
            swal("当前为第一页!");
            return;
        }
        Count_vm.pageOrderCurrent--;
        Count_vm.cheakOder(Count_vm.currentItem);
    },
    previousPage: function () {
        if (Count_vm.pageCurrent == 1) {
            swal("当前为第一页!");
            return;
        }
        Count_vm.pageCurrent--;
        Count_vm.getUserList();
    },
    OrdernextPage: function () {
        if (Count_vm.pageOrderCurrent == Count_vm.pageOrderTotle) {

            swal("当前为最后一页!");
            return;
        }
        Count_vm.pageOrderCurrent++;
        Count_vm.cheakOder(Count_vm.currentItem);
    },
    nextPage: function () {
        if (Count_vm.pageCurrent == Count_vm.pageTotle) {

            swal("当前为最后一页!");
            return;
        }
        Count_vm.pageCurrent++;
        Count_vm.getUserList();
    }
});
Count_vm.getUserList();